<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>姓名案例-原生实现</title>
</head>
<body>
    <script>
        let p = {
            firstName: '张',
            lastName: '三'
        }

        Object.defineProperty(p,'fullName',{
            set(){ //fullName被修改时,set被调用.且set方法中的this是p.且set会收到修改的值.
                const arr = value.split('-')
                this.firstName = arr[0]
                this.lastName = arr[1]
            },
            get(){ //fullName被读取时,get被调用 且get方法中的this是p
                return this.firstName + '-' + this.lastName
            }
        })
        //当firstName被修改时,fullName也会获得最新的.
        //同理,lastName被修改时,fullName也会获得最新值.

    </script>
</body>
</html>